<?php

namespace App\ZhCN\Api;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use Phoenix\Http\RequestMethod;
use App\Tools\Auxi;
use Phoenix\Support\MsgHelper;
use Phoenix\Log\Log4p as logger;

class Common{

    private function __RestController() {}

    private function __Route($value = '/api/common') {}

    protected function __Inject($cache, Service\UPFile $upFile, Service\Mobile $serMobile, Repository\Member $repoMember) {}

    private $_expire = 1;  //验证码的过期时间 1分钟

    //文件上传
    public function upload() {
        $_successImg = [];
        $_failImg = [];
        if (count($_FILES) > 0) {
            foreach ($_FILES as $key => $error) {
                $_resource = $this->upFile->user(1, $key);
                $errorAry = explode('_', $key);
                $_flag = false;
                if ($_resource) {
//                    var_dump($_resource);die;
                    if ($this->upFile->reservationOrderFile($_resource)) {
                        $_resource['filename'] = $key;
                        $_resource['item_id'] = isset($errorAry[1]) ? $errorAry[1] : ' ';
                        //$_successImg[] = $_resource;
                        $_successImg[] = array('name' => $_resource['name'], 'size' => $_resource['size']);
                    } else {
                        $_flag = true;
                    }
                } else {
                    $_flag = true;
                }
                if ($_flag) {
                    $_failImg[] = array('item_id' => $errorAry[1]);
                }
            }
        }
        return MsgHelper::err(0, '', array('success' => $_successImg, 'fail' => $_failImg, 'root' => $this->root));
    }


    /**
     * @Title: sendMobileCode
     * @Description: TODO 获取手机验证码
     * @return array
     * @author TUGE
     * @date
     */
    public function sendMobileCode($__Method = RequestMethod::POST) {
            Auxi::checkMustFieldT(array('mobile', 'type'), $_POST);
            Auxi::isValidMobile(trim($_POST['mobile']));

            if ($_POST['type'] == 0) {
                //用户注册
                $cacheName = 'registeMemberCode_';
            } else if ($_POST['type'] == 1) {
                //快速登录
                $cacheName = 'SoonLoginCode_';
            } else if ($_POST['type'] == 2) {
                //忘记密码
                $cacheName = 'forgetLoginCode_';
            } else if ($_POST['type'] == 3) {
                //修改资料
                $cacheName = 'SoonLoginCode_';
            }

            //1分钟内不重复发送
            $_codeStr = $this->cache->expires($this->_expire * 60)->get($cacheName . $_POST['mobile']);
            if ($_codeStr) {
                return MsgHelper::err(20001,'',$_codeStr);
            }


            //查询该手机号的工匠数量
            if ($_POST['type'] == 0) {
                $countMember = $this->repoMember->countMember($_POST['mobile']);
                //注册发送验证码
                if ($countMember > 0) {
                    return MsgHelper::err(20000, '该手机号已注册!');
                }

            } elseif (in_array($_POST['type'], array(1, 2))) {
                $countMember = $this->repoMember->countMember($_POST['mobile']);

                //忘记密码和快速登录发送验证码
                if ($countMember == 0) {
                    return MsgHelper::err(20000, '该手机号暂未注册!');
                }
            }


            $_POST['mobile'] = trim($_POST['mobile']);
            $_code = mt_rand(100000, 999999);
            $this->cache->expires($this->_expire * 60)->set($cacheName . $_POST['mobile'], $_code);
            //1分钟内有效
            $_rs = $this->serMobile->sendContent($_POST['mobile'], $_code, 1);
            logger::debug($_rs);
            if ($_rs->code == 0) {
//            if (1 == 1) {
                return MsgHelper::err(0, '', array('code'=>$_code));
            } else {
                return MsgHelper::err(-1, '', array('code'=>$_code));
            }

    }

}
